package com.changgou.goods.service.impl;

import com.changgou.entity.PageResult;
import com.changgou.goods.dao.CategoryMapper;
import com.changgou.goods.dao.SpecMapper;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Spec;
import com.changgou.goods.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private SpecMapper specMapper;

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<String> selectAll() {
        return specMapper.fedAll();
    }

    /**
     * 根据id删除规格
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        specMapper.deleteByPrimaryKey(id);
    }

    /**
     * 添加规格
     * @param spec
     */
    @Override
    public void add(Spec spec) {
        specMapper.insertSelective(spec);
    }

    /**
     * 查询规格列表数据
     * @param id
     * @param page
     * @param size
     * @return
     */
    public PageResult<Spec> findPageById(int id , int page, int size){
        //获取总记录数
        Long tatol = specMapper.findCountById(id);
        //计算开始条数
        int start = (page - 1) * size;
        //获取当前页数据集合
        List<Spec> specList = specMapper.findPageById(id,start,size);
        //返回数据
        return new PageResult<Spec>(tatol,specList);
    }

    @Override
    public List<Spec> findByCategory(Integer categoryId) {
        //知道分类id
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        //跟据template_id查询规格分类
        Spec spec=new Spec();
        spec.setTemplateId(category.getTemplateId());
        return specMapper.select(spec);
    }


}
